// pages/second_level/suggest/suggest.js
Page({
  data:{
    showFilePath:'',
    status:'',
    textarea:'',
    ownerName:'',
    communityName:'',
    userInfo:[]
    
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    let userInfo=wx.getStorageSync('userInfo');
    if(userInfo){
      userInfo =JSON.parse(userInfo);
      this.setData({
        ownerName:userInfo.ownerName,
        communityName:userInfo.communityName,
        userInfo:userInfo
      })
      console.log(this.data.ownerName);
    }

  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  uploadImage() {
    let that=this;
    let fileArrays = [];
    wx.chooseMedia({
      success: function (res) {
        console.log(res);
        // tempFiles  数组  tempFilePath 数组图片路径
        let localFiles = res.tempFiles;
  
        wx.showLoading({
          title: '正在上传',
        })
        console.log("$$$$$$$")
        console.log(localFiles.length)
        console.log("$$$$$$$")
        //数组遍历
        for (let i = 0; i < localFiles.length; i++) {
          let localPath = localFiles[i].tempFilePath;
            fileArrays.push(localPath)
            console.log(fileArrays);
            // that.setData({
            //   showFilePath:fileArrays
            // })
          wx.uploadFile({ 
            url:'http://localhost/file',  
            header: { "Content-Type": "multipart/form-data" },
            name:'file',
            filePath: localPath, 
            success: function (res) {
              wx.showToast({
                title: '上传成功',
              })
              console.log("$$$$$$$")
              that.setData({
                showFilePath:""+res.data
              })
              console.log(that.data.showFilePath)
              console.log(res.data);
            console.log("$$$$$$$")
            },
            fail(error){
              wx.showToast({
                title: '上传失败',
              })
            }
          })
        }
      }
    })
  },
  previewImg(e){
    console.log(e);
    var showFilePath=this.data.showFilePath;
    var index=e.target.dataset.index;
    console.log(index);
    wx.previewImage({
      current:showFilePath[index],
      urls: showFilePath,
    })
  },
  removeImage(e){
    this.setData({
      showFilePath:''
    })

  },
  handlechange(e){
    console.log(e);
    this.setData({
      status:e.detail.value
    })
  },

  bindFormSubmit: function(e) {
    if(this.data.userInfo.ownerName){
      let that=this;
        let complaintImage=this.data.showFilePath;
        let status=this.data.status;
        let ownerName=this.data.ownerName;
        let communityName=this.data.communityName;
        let description=e.detail.value.textarea;
        // 当前日期时间  YYYY-MM-DD hh:mm:ss    
        let createTime=new Date().toJSON().substring(0, 10) + ' ' + new Date().toTimeString().substring(0,8); 
        wx.request({
          url: 'http://localhost/complaint/addComplaint',
          data:{
            complaintImage:complaintImage,
            status:status,
            description:description,
            ownerName:ownerName,
            communityName:communityName,
            createTime:createTime
          },
          success(result){
          console.log(result);
        
          that.setData({
                status:'',
                description:'',
                showFilePath:''            
              })
        
              if(result.data.code==0){
              
              wx.showToast({
                title: result.data.msg,         
              })
            
            }else{
            
              wx.showToast({
                icon:'error',
                title: result.data.msg,
              })
            }

          }
        })

    }else{  
      wx.navigateTo({
        url: '../userBind/userBind',
    })
      wx.showToast({
        icon:'none',
        title: '请认证您的身份',
      })
    }
  
  },

})